package com.yangdm.service;

import com.yangdm.entity.TableFieldsAndTypesEntity;
import com.yangdm.entity.TableForeignKeyEntity;
import com.yangdm.entity.TableIndexEntity;
import com.yangdm.entity.TablePrimaryKeyEntity;
import com.yangdm.mapper.*;
import com.yangdm.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.yangdm.util.SQLScriptGenerator.*;

/**
 * @program: oracle2mysql_tool
 * @description: Oracle2MySQL迁移服务层
 * @author: yangdm
 * @create: 2024-12-24 15:58
 **/
@Service(value = "oracle2MySQLService")
public class Oracle2MySQLService {

    private final Logger logger = LoggerFactory.getLogger(Oracle2MySQLService.class);

    @Autowired
    @Qualifier("tableMapper")
    private TableMapper tableMapper;

    @Autowired
    private TableFieldsAndTypesMapper fieldsAndTypesMapper;

    @Autowired
    private TableForeignKeyMapper foreignKeyMapper;

    @Autowired
    private TableIndexMapper indexMapper;

    @Autowired
    private TablePrimaryKeyMapper primaryKeyMapper;

    @Value("${generate.table_structure_script_path}")
    private String generateTableStructureScriptPath;

    @Value("${generate.table_foreign_key_script_path}")
    private String generateTableForeignKeyScriptPath;

    @Value("${generate.table_charset}")
    private String tableCharset;

    @Value("${generate.table_collation}")
    private String tableCollation;

    /**
     * 生成表外键脚本到指定文件
     */
    public void generateTableStructureSQLScripts(){
        // 清空文件内容，避免重复写入
        FileUtils.clearFileContent(generateTableStructureScriptPath);
        List<String> tableNames = this.tableMapper.findAllName();
        for (int i = 0; i < tableNames.size(); i++) {
            String tableName = tableNames.get(i);
            // 转化MySQL语法
            List<TableFieldsAndTypesEntity> fieldsAndTypesList = this.fieldsAndTypesMapper.findByTableName(tableName);
            List<TablePrimaryKeyEntity> primaryKeyList = this.primaryKeyMapper.findByTableName(tableName);
            List<TableIndexEntity> indexList = this.indexMapper.findByTableName(tableName);
            String sqlScripts = generateDropAndCreateTableSQLScripts(tableName, fieldsAndTypesList, primaryKeyList, indexList, tableCharset, tableCollation);
            // 打印SQL脚本
            logger.info(sqlScripts);
            logger.info("第{}张表 `{}`，已生成完毕\r\n", (i+1), tableName);
            // 将SQL脚本写入文件
            FileUtils.appendLineToFile(generateTableStructureScriptPath, sqlScripts);
        }
    }

    /**
     * 生成表外键脚本到指定文件
     */
    public void generateTableForeignKeySQLScripts() {
        // 清空文件内容，避免重复写入
        FileUtils.clearFileContent(generateTableForeignKeyScriptPath);
        List<String> tableNames = this.tableMapper.findAllName();
        for (int i = 0; i < tableNames.size(); i++) {
            String tableName = tableNames.get(i);
            List<TableForeignKeyEntity> foreignKeyList = this.foreignKeyMapper.findByTableName(tableName);
            String sqlScripts = generateForeignKeySQLScripts(tableName, foreignKeyList);
            // 打印SQL脚本
            logger.info(sqlScripts);
            logger.info("第" + (i+1) + "张表 `" + tableName + "` 的外键脚本已生成完毕\r\n");
            // 将SQL脚本写入文件
            FileUtils.appendLineToFile(generateTableForeignKeyScriptPath, sqlScripts);
        }
    }

}
